package com.siyu.rehearsal.grpc;

import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class WriteToCSV {

    public static void main(String[] args) throws IOException, ParseException {
        ArrayList<Double> qt = new ArrayList<>();
        ArrayList<String> tms = new ArrayList<>();

        // 示例数据填充，实际使用时应根据实际情况填充数据
        qt.add(0.01);
        qt.add(0.02);
        tms.add("2023-06-06 01:00");
        tms.add("2023-06-06 02:00");

        writeDataToCSV(qt, tms, "output.csv");
    }

    public static void writeDataToCSV(ArrayList<Double> qt, ArrayList<String> tms, String fileName) throws IOException, ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        FileWriter writer = new FileWriter(fileName);

        // 写入表头，根据您的描述调整
        writer.append("Yr,Mth,D,Hr,Min,Q_ID1_cms\n");

        // 确保两个列表长度相同
        int size = Math.min(qt.size(), tms.size());
        for (int i = 0; i < size; i++) {
            Date date = dateFormat.parse(tms.get(i)); // 将字符串转换为Date对象以便获取年月日时分
            String year = String.valueOf(date.getYear() + 1900); // 注意，getYear()返回的是1900年前的年数
            String month = String.format("%02d", date.getMonth() + 1); // 月份从0开始，需要+1并格式化为两位
            String day = String.format("%02d", date.getDate()); // 日期也需要格式化为两位
            String hour = String.format("%02d", date.getHours());
            String minute = String.format("%02d", date.getMinutes());

            // 拼接一行数据
            writer.append(year + ",");
            writer.append(month + ",");
            writer.append(day + ",");
            writer.append(hour + ",");
            writer.append(minute + ",");
            writer.append(qt.get(i).toString() + "\n"); // 假设qt中的数据对应Q_ID1_cms
        }

        writer.flush();
        writer.close();
    }

    public static void write15DataToCSV(ArrayList<ArrayList<Double>> data, ArrayList<String> tms, String fileName) throws IOException, ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        FileWriter writer = new FileWriter(fileName);
        ArrayList<String> identifiers = new ArrayList<>();
        for (int i = 1; i <= data.size(); i++) {
            String name = "Q_ID" + i + "_cms";
            identifiers.add(name);
        }
        // 写入表头
        writer.append("Yr,Mth,D,Hr,Min,");
        writer.append(String.join(",", identifiers));
        writer.append("\n");

        // 确保所有标识的数据列表和时间戳列表长度相同
        int size = tms.size();
      /*  for (ArrayList<Double> list : data) {
            size = Math.min(size, list.size());
        }*/

        // 将数据转换为Map，键为标识符，值为数据列表
        Map<String, ArrayList<Double>> dataMap = new HashMap<>();
        for (int i = 0; i < identifiers.size(); i++) {
            dataMap.put(identifiers.get(i), data.get(i));
        }

        for (int i = 0; i < size; i++) {
            String datatm = tms.get(i);
          /*  Date date = dateFormat.parse(tms.get(i));
            String year = String.valueOf(date.getYear() + 1900);
            String month = String.format("%02d", date.getMonth() + 1);
            String day = String.format("%02d", date.getDate());
            String hour = String.format("%02d", date.getHours());
            String minute = String.format("%02d", date.getMinutes());*/

            String year = datatm.split(" ")[0].split("-")[0];
            String month = datatm.split(" ")[0].split("-")[1];
            String day = datatm.split(" ")[0].split("-")[2];
            String hour = datatm.split(" ")[1].split(":")[0];
            String minute = datatm.split(" ")[1].split(":")[1];

            // 拼接一行数据
            writer.append(year + "," + month + "," + day + "," + hour + "," + minute+",");

            boolean isFirst = true;
            for (String key : identifiers) {
                ArrayList<Double> values = dataMap.get(key);
                if (i < values.size()) {
                    if (!isFirst) {
                        writer.append(",");
                    }
                    writer.append(values.get(i).toString());
                    isFirst = false;
                } else {
                    if (!isFirst) {
                        writer.append(",");
                    }
                    isFirst = false;
                }
            }

            writer.append("\n"); // 在每行数据后写入换行符
        }

        writer.flush();
        writer.close();
    }
}